#pragma once
#include"bitset.h"
#include<time.h>
#include<math.h>
using namespace std;
namespace mutou{
struct HashFuncBKDR
{
	// @detail 本 算法由于在Brian Kernighan与Dennis Ritchie的《The CProgramming Language》
	// 一书被展示而得 名，是一种简单快捷的hash算法，也是Java目前采用的字符串的Hash算法累乘因子为31。
	size_t operator()(const std::string& s)
	{
		size_t hash = 0;
		for (auto ch : s)
		{
			hash *= 31;
			hash += ch;
		}
		return hash;
	}
};
struct HashFuncAP
{
	// 由Arash Partow发明的一种hash算法。  
	size_t operator()(const std::string& s)
	{
		size_t hash = 0;
		for (size_t i = 0; i < s.size(); i++)
		{
			if ((i & 1) == 0) // 偶数位字符
			{
				hash ^= ((hash << 7) ^ (s[i]) ^ (hash >> 3));
			}
			else              // 奇数位字符
			{
				hash ^= (~((hash << 11) ^ (s[i]) ^ (hash >> 5)));
			}
		}

		return hash;
	}
};
struct HashFuncDJB
{
	// 由Daniel J. Bernstein教授发明的一种hash算法。 
	size_t operator()(const std::string& s)
	{
		size_t hash = 5381;
		for (auto ch : s)
		{
			hash = hash * 33 ^ ch;
		}

		return hash;
	}
};
template<
size_t N
,size_t X =5
,class K =std::string
,class HASH1=HashFuncBKDR
,class HASH2=HashFuncAP
,class HASH3=HashFuncDJB>
class bloomfilter
{
    public:
        void Set(const K& n)
        {
            //防止哈希溢出
            size_t hash1 = HASH1()(n)%N;
            size_t hash2 = HASH2()(n)%N;
            size_t hash3 = HASH3()(n)%N;
            _bs.set(hash1);
            _bs.set(hash2);
            _bs.set(hash3);
    }
        bool Test(const K& n)
        {
            size_t hash1 = HASH1()(n)%N;
            if(!_bs.test(hash1))
            {
                return false;
            }
            size_t hash2 = HASH2()(n)%N;
            if(!_bs.test(hash2))
            {
                return false;
            }
            size_t hash3 = HASH3()(n)%N;
            if(!_bs.test(hash3))
            {
                return false;
            }
            return true;
        }
        double getFalseProbability()
	{
		double p = pow((1.0 - pow(2.71, -3.0 / X)), 3.0);

		return p;
	}
    private:
    const static int M = N*X;
    bitset<M> _bs;
};
    void testbloomfilter()
    {
        bloomfilter<100> bf;
        bf.Set("donk");
        bf.Set("faker");
        bf.Set("donk1");
        bf.Set("donk2");
        bf.Set("donk3");
        std::cout<<bf.Test("donk")
        <<bf.Test("faker")
        <<" "<<bf.Test("father")
        <<" "<<bf.Test("donk3")
        <<" "<<bf.Test("2222222222222222222222222222222222d1onk777777777777777");


    }

void TestBloomFilter2()
{
	srand(time(0));
	const size_t N = 1000000;
	bloomfilter<N> bf;
	//bloomfilter<N, 3> bf;
	//bloomfilter<N, 10> bf;

	std::vector<std::string> v1;
	//std::string url = "https://www.cnblogs.com/-clq/archive/2012/05/31/2528153.html";
	//std::string url = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=65081411_1_oem_dg&wd=ln2&fenlei=256&rsv_pq=0x8d9962630072789f&rsv_t=ceda1rulSdBxDLjBdX4484KaopD%2BzBFgV1uZn4271RV0PonRFJm0i5xAJ%2FDo&rqlang=en&rsv_enter=1&rsv_dl=ib&rsv_sug3=3&rsv_sug1=2&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=330&rsv_sug4=2535";
	std::string url = "donk";

	for (size_t i = 0; i < N; ++i)
	{
		v1.push_back(url + std::to_string(i));
	}

	for (auto& str : v1)
	{
		bf.Set(str);
	}

	// v2跟v1是相似字符串集（前缀一样），但是后缀不一样
	v1.clear();
	for (size_t i = 0; i < N; ++i)
	{
		std::string urlstr = url;
		urlstr += std::to_string(9999999 + i);
		v1.push_back(urlstr);
	}

	size_t n2 = 0;
	for (auto& str : v1)
	{
		if (bf.Test(str)) // 误判
		{
			++n2;
		}
	}
	cout << "fair%:" << (double)n2 / (double)N << endl;

	// 不相似字符串集  前缀后缀都不一样
	v1.clear();
	for (size_t i = 0; i < N; ++i)
	{
		//string url = "zhihu.com";
		string url = "faker";
		url += std::to_string(i + rand());
		v1.push_back(url);
	}

	size_t n3 = 0;
	for (auto& str : v1)
	{
		if (bf.Test(str))
		{
			++n3;
		}
	}
	cout << "unfair%:" << (double)n3 / (double)N << endl;

	cout << "gongshifair%:" << bf.getFalseProbability() << endl;
}
}